package com.trainoo.mina.handler;

import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @Auther: zt
 * @Date: 2020/4/13 17:28
 * @Description:
 */
public class MyClientHandler extends IoHandlerAdapter {

    private static final Logger LOG = LoggerFactory.getLogger(MyClientHandler.class);

    public MyClientHandler() {
        super();
    }

    @Override
    public void exceptionCaught(IoSession session, Throwable cause) throws Exception {
        LOG.error("客户端[session={}]发生异常", session.getId(), cause);
        session.close(true);
    }

    @Override
    public void messageReceived(IoSession session, Object message) throws Exception {
        LOG.info("客户端[{}]接收到数据：{}", session.getId(), message);
    }

    @Override
    public void sessionIdle(IoSession session, IdleStatus status) throws Exception {
        // 空闲时，关闭session
        if (status == IdleStatus.BOTH_IDLE) {
            LOG.info("session进入空闲，准备关闭session");
            session.close(false);
        }
    }
}
